package cn.zjxiliu.lib_core.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.support.annotation.NonNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


/**
 * Created by jessie on 2018/10/25.
 */

public class AssertsResUtils {


    public static InputStream getConfigInputStream(Context context, @NonNull String configName) throws IOException {
        AssetManager assetManager = context.getAssets();
        String[] fileNames = assetManager.list("");
        if (fileNames != null && fileNames.length > 0) {
            for (String fileName : fileNames) {
                if (configName.equalsIgnoreCase(fileName)) {
                    return assetManager.open(fileName, AssetManager.ACCESS_BUFFER);
                }
            }
        }
       return null;
    }


    public static Properties getConfigProperties(Context context, @NonNull String configName) throws IOException {
        AssetManager assetManager = context.getAssets();
        String[] fileNames = assetManager.list("");
        if (fileNames != null && fileNames.length > 0) {
            for (String fileName : fileNames) {
                if (configName.equalsIgnoreCase(fileName)) {
                    Properties properties = new Properties();
                    properties.load(context.getAssets().open(configName));
                    return properties;
                }
            }
        }
        return null;
    }

}
